<template>
  <view class="content">
    <view style="background: #fff">
      <basics-form ref="basicsForm" @submit="submit" @selectCallback="selectCallback" />
    </view>
    <view style="height: 200rpx" />
    <foot-button
      v-if="showButton && role != 5"
      style="position: fixed; bottom: 0rpx; z-index: 10"
      :buttonList="buttonList"
      @tapButton="$refs.basicsForm.formValid()"
    />
  </view>
</template>

<script>
import basicsForm from "@/components/basics-form/basics-form.vue";
import footButton from "@/components/foot-button/foot-button.vue";

export default {
  components: {
    basicsForm,
    footButton,
  },
  data() {
    return {
      patientId: null,
      tipsDetail: null,
      frequently: null,
      listFamily: null,
      type: 0,
      role: uni.getStorageSync("role"),
      showButton: false,
      buttonList: [
        {
          name: `保存`,
          icon: "save",
        },
      ],
    };
  },
  onLoad(options) {
    this.type = options.type; //1上传提醒 2患者关怀 3日常提醒
    if (this.type == 2) {
      if (options.isDetail) {
        this.tipsDetail = uni.getStorageSync("tipsDetail");
        uni.setNavigationBarTitle({ title: this.tipsDetail.isHandle ? "关怀详情" : "处理患者关怀" });
        if (this.tipsDetail.isHandle) {
          this.initRules();
        } else {
          this.getFrequently();
          this.showButton = true;
        }
      }
    } else if (this.type == 3 || this.type == 1 || this.type == 4) {
      if (options.isDetail) {
        this.tipsDetail = uni.getStorageSync("tipsDetail");
        uni.setNavigationBarTitle({ title: "提醒详情" });
        this.initRules();
      } else {
        uni.setNavigationBarTitle({ title: "发送日常提醒" });
        this.patientId = options.patientId;
        this.getFrequently();
        this.showButton = true;
      }
    }
    if (this.role == 5) {
      uni.setNavigationBarTitle({ title: "消息详情" });
    }
  },
  methods: {
    //常用语 type==2?关怀：提醒
    getFrequently() {
      this.frequently = uni.getStorageSync(this.type == 2 ? "careReason" : "remindWords");
      if (this.frequently) {
        if (this.type == 2) {
          this.initRules();
        } else {
          this.doctorListFamily();
        }
      } else {
        this.$api[this.type == 2 ? "doctorCareReasonList" : "doctorRemindWordsList"]().then((res) => {
          if (res.code == 200) {
            this.frequently = res.data;
            if (this.type == 2) {
              this.initRules();
            } else {
              this.doctorListFamily();
            }
            uni.setStorage({
              key: this.type == 2 ? "careReason" : "remindWords",
              data: res.data,
            });
          }
        });
      }
    },
    //紧急联系人
    doctorListFamily() {
      this.$api.doctorListFamily({ patientId: this.patientId }).then((res) => {
        if (res.code == 200) {
          this.listFamily = res.data.map((item) => {
            item.name += `(${item.relation})`;
            return item;
          });
          this.initRules();
        }
      });
    },
    initRules() {
      let rules = null;
      console.log(this.tipsDetail);
      if (this.type == 2) {
        rules = [
          {
            title: "详情",
            key: "content",
            value: this.tipsDetail ? this.tipsDetail.content : "",
            type: "textarea",
            disabled: true,
            skip: true,
          },
          {
            title: "常用语",
            key: "word",
            type: "select",
            selectList: this.frequently || [],
            selectKey: "dictValue",
            selectShowKey: "dictValue",
            skip: true,
            callback: true,
            hidden: !this.frequently || this.role == 5,
          },
          {
            title: "处理",
            key: "handleResult",
            value: this.tipsDetail ? this.tipsDetail.handleResult : "",
            type: "textarea",
            disabled: this.tipsDetail && this.tipsDetail.isHandle ? true : false,
            skip: this.tipsDetail && this.tipsDetail.isHandle ? true : false,
            hidden: this.role == 5,
          },
          {
            title: "发送时间",
            key: "sendTime",
            value: this.tipsDetail && this.tipsDetail.sendTime ? this.tipsDetail.sendTime.substring(0, 16) : "",
            disabled: true,
            skip: true,
            hidden: !this.tipsDetail.sendTime,
          },
          {
            title: "处理时间",
            key: "handleTime",
            value: this.tipsDetail && this.tipsDetail.handleTime ? this.tipsDetail.handleTime.substring(0, 16) : "",
            disabled: true,
            skip: true,
            hidden: !this.tipsDetail.handleTime || this.role == 5,
          },
        ];
      } else if (this.type == 3 || this.type == 1 || this.type == 4) {
        rules = [
          {
            title: "常用语",
            key: "word",
            type: "select",
            selectList: this.frequently || [],
            selectKey: "dictValue",
            selectShowKey: "dictValue",
            skip: true,
            callback: true,
            hidden: !this.frequently,
          },
          {
            title: "详情",
            key: "content",
            value: this.tipsDetail ? this.tipsDetail.content : "",
            type: "textarea",
            disabled: this.tipsDetail ? true : false,
            skip: this.tipsDetail ? true : false,
          },
          {
            title: "通知亲友",
            key: this.tipsDetail ? "familyNames" : "familyIds",
            type: this.tipsDetail ? "textarea" : "choose",
            selectList: this.listFamily || [],
            selectKey: "familyId",
            value: this.tipsDetail ? this.tipsDetail.familyNames || "" : [],
            disabled: this.tipsDetail ? true : false,
            skip: true,
            hidden: this.type == 1 || this.type == 4||(this.tipsDetail&&(!this.listFamily||!this.listFamily.length)),
          },
          {
            title: "发送时间",
            key: "sendTime",
            value: this.tipsDetail && this.tipsDetail.sendTime ? this.tipsDetail.sendTime.substring(0, 16) : "",
            disabled: true,
            skip: true,
            hidden: !this.tipsDetail || !this.tipsDetail.sendTime,
          },
        ];
      }

      this.$nextTick(() => {
        this.$refs.basicsForm.initForm(rules);
      });
    },
    //选择器回调
    selectCallback(res) {
      if (res.key == "word" && res.value) {
        this.$refs.basicsForm.changeParamValue({ key: this.type == 2 ? "handleResult" : "content", value: res.value });
      }
    },

    submit(param) {
      if (!param) {
        return;
      }
      uni.showModal({
        title: "提示",
        content: "确认提交？",
        success: (res1) => {
          if (res1.confirm) {
            let data = null;
            if (this.type == 2) {
              data = {
                reId: this.tipsDetail.reId,
                handleResult: param.handleResult,
              };
            } else {
              data = {
                patientId: this.patientId,
                content: param.content,
                familyIds: param.familyIds.join(","),
              };
            }
            this.$api[this.type == 2 ? "doctorHandleCareMsg" : "doctorAddRemind"](data).then((res) => {
              if (res.code == 200) {
                this.$util.showSuccessToast("已提交");
                setTimeout(() => {
                  this.$util.backToRefreshPrevPage("tips/index");
                }, 1500);
              }
            });
          }
        },
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.content {
  background: $back-grey-color;
  min-height: 100vh;
}
</style>
